package ace.cmp.util;

import ace.cmp.core.exception.BusinessException;
import java.util.Collection;
import java.util.Objects;
import java.util.function.Supplier;

/**
 * @author Caspar
 * @create 2021/2/24 断言检查，抛出{@link BusinessException} 异常
 */
public class AceAssertUtils {

  public static void isNull(Object value) {
    isNull(value, () -> new IllegalArgumentException());
  }

  public static void isNull(Object value, String messageTemplate, Object... args) {
    String message = String.format(messageTemplate, args);
    isNull(value, message);
  }

  public static void isNull(Object value, String message) {
    if (Objects.isNull(value)) {
      throw new BusinessException(message);
    }
  }

  public static void isNull(Object value, Supplier<RuntimeException> supplier) {
    if (Objects.isNull(value)) {
      throw supplier.get();
    }
  }

  public static void isNotNull(Object value) {
    isNotNull(value, () -> new IllegalArgumentException());
  }

  public static void isNotNull(Object value, String messageTemplate, Object... args) {
    isTrue(value != null, messageTemplate, args);
  }

  public static void isNotNull(Object value, String message) {
    isTrue(value != null, message);
  }

  public static void isNotNull(Object value, Supplier<RuntimeException> supplier) {
    if (value != null) {
      throw supplier.get();
    }
  }

  public static void isTrue(boolean value) {
    isTrue(value, () -> new IllegalArgumentException());
  }

  public static void isTrue(boolean value, String messageTemplate, Object... args) {
    String message = String.format(messageTemplate, args);
    isTrue(value, message);
  }

  public static void isTrue(boolean value, String message) {
    isTrue(value, () -> new BusinessException(message));
  }

  public static void isTrue(boolean value, Supplier<RuntimeException> supplier) {
    if (value) {
      throw supplier.get();
    }
  }

  public static void isFalse(boolean value) {
    isFalse(value, () -> new IllegalArgumentException());
  }

  public static void isFalse(boolean value, String messageTemplate, Object... args) {
    isTrue(!value, messageTemplate, args);
  }

  public static void isFalse(boolean value, String message) {
    isTrue(!value, message);
  }

  public static void isFalse(boolean value, Supplier<RuntimeException> supplier) {
    isTrue(!value, supplier);
  }

  public static void isEmpty(String value, String message) {
    if (value == null || value.length() == 0) {
      throw new BusinessException(message);
    }
  }

  public static void isEmpty(String value, String messageTemplate, Object... args) {
    isEmpty(value, String.format(messageTemplate, args));
  }

  public static void isEmpty(Collection value, String message) {
    if (value == null || value.size() == 0) {
      throw new BusinessException(message);
    }
  }

  public static void isEmpty(Collection value, String messageTemplate, Object... args) {
    isEmpty(value, String.format(messageTemplate, args));
  }
}
